package pl.edu.agh.iosr.gamblingzone.dao.hibernate;

import java.util.List;

import org.hibernate.criterion.Restrictions;

import pl.edu.agh.iosr.gamblingzone.dao.UserDAO;
import pl.edu.agh.iosr.gamblingzone.model.User;

/**
 * The Class UserDAOBean.
 */
public class UserDAOBean  extends GenericDAOBean<User, Long>
		implements UserDAO {

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.UserDAO#authenticateUser(java.lang.String, java.lang.String)
	 */
	public boolean authenticateUser(String login, String password) {
		User u = findByLogin(login);
		
		if (u != null && u.getPassword().equals(User.computeSHA1(password))) {
			return true;
		}
		
		return false;
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.UserDAO#findByLogin(java.lang.String)
	 */
	public User findByLogin(String login) {
		
		List<User> userList = findByCriteria(
				(new ComplexCriterion()).add(Restrictions.like("loginName", login)));
		
		if (userList != null && userList.size() > 0) {
			return userList.get(0);
		}
		
		return null;
	}
}
